// this file collapses the kid-parent-wave file to kid-wave level and creates those events
clear
clear matrix
clear mata

set maxvar 30000

// get full kid-parent-wave data
use data/constructed_data/kid_parent_wave_all.dta, replace

// create kid-wave level events

// # of parents
bys kidid_group wave: gen num_parents = _N

drop if mi(kidid_group)

// is the parent male?
gen is_male = (ragender == 1)

// is kid female?
gen kid_is_female = kgender == 2

//make a list of parent-kid-wave level events;
gen rcardiac = cond(rstrok_dummy == 1 | rheart == 1, 1, 0)
replace rcardiac = . if mi(rstrok_dummy) | mi(rheart)

//create memory event
gen rmemory = rmemry == 1 | ralzhe == 1 | rdemen == 1
replace rmemory = . if wave < 4
replace rmemory = . if wave >=4 & wave<10 & mi(rmemry)
replace rmemory = . if wave >= 10 & (mi(ralzhe) | mi(rdemen))

gen public_ins = cond(rgovmd == 1 | rgovmr == 1 | rgovva == 1, 1, 0)

#delim ;
local pk_events rlost_job rlost_job_dis jl_reason jl_invol jl_badhealth rretired 
rdivorced rwidowed health3to5 rhosp rstrok_dummy rcardiac any_radla rhlthlm rmemory 
rcancr rgovmd rgovmr public_ins ;

#delim cr 

// loop through them to create kid-wave household events
foreach e in `pk_events'{
//  generate flag for any parents missing a cod
//	 count the number of nonmissings
	egen nomi_`e' = count(`e'), by(kidid_group wave)
//	set missing flag to 1 if number of nonmissings doesn't match group #
	gen anymi_`e'   = nomi_`e' != num_parents
	
//	total # parent events
	egen total_`e' = sum(`e'), by(kidid_group wave)
	replace total_`e' = . if anymi_`e' == 1

//	any parent event
	gen any_`e' = cond(total_`e' > 0, 1, 0)
	replace any_`e' = . if mi(`e')  // needed because . > 0 is true

//  all parents experienced event
	gen all_`e' = (total_`e' == num_parents)
	replace all_`e' = . if anymi_`e' == 1

//	male parent event
	gen is_male_`e' = is_male*`e'
	egen male_`e' = sum(is_male_`e') if !mi(is_male_`e'), by(kidid_group wave)

	drop is_male_`e'
	drop nomi_`e'
	drop anymi_`e'
}

// make a list of transfer vars; eventually include helping behaviors, living close etc.
local transfers real_kfcamt real_ktcamt

foreach t in `transfers'{
//  generate flag for any parents missing
//	count the number of nonmissings
	egen nomi_`t' = count(`t'), by(kidid_group wave)

//	set missing flag to 1 if number of nonmissings doesn't match group #
	gen anymi_`t'   = nomi_`t' != num_parents
	tab anymi_`t', m

//   total
	egen total_`t' = sum(`t'), by(kidid_group wave)
	replace total_`t' = . if anymi_`t' == 1

//   average 
	egen mean_`t'  = mean(`t'), by(kidid_group wave)
	replace mean_`t' = . if mi(`t')
//   any 
	gen any_`t'    = `t' > 0
	replace any_`t' = . if mi(`t') // needed because . > 0 is true

	drop nomi_`t'
	drop anymi_`t'
}

// parent-sample variables so that later we can subset by parent age
// may eventually include parent insurance status or other vars
egen max_page = max(ragey_e) , by(kidid_group wave)
egen min_page = min(ragey_e) , by(kidid_group wave)
egen oldest_age = max(ragey_e), by(kidid_group wave)


gen ris_nonwhite = cond(raracem != 1, 1, 0)
replace ris_nonwhite = . if mi(raracem)
egen any_nonwhite = max(ris_nonwhite), by(kidid_group wave)
egen highest_ed_yr = max(raedyrs), by(kidid_group wave)
egen any_live_nrshm = max(rnhmliv), by(kidid_group wave)

// create wealth shock variables since they're all measured at the household level
egen cohortXwave = group(hacohort wave)

// kid works
gen kwork_ft = kwork == 2 if !mi(kwork)
gen kwork_pt = kwork == 1 if !mi(kwork)
gen kwork_atall = kwork != 0 if !mi(kwork)

// total kid FTEs
gen fem_rwidowed = any_rwidowed-male_rwidowed
gen bad_health = cond(rshlt > 3, 1, 0)

gen has_disability = cond(radla > 0 | riadla > 0, 1, 0)
egen any_bad_health = max(bad_health), by(kidid_group wave)

egen any_disability = max(has_disability), by(kidid_group wave)

rename has_disability  disab
rename any_disability any_disab

// both parents between 50 and 74 and in at least 6 waves
gen AGE_50_74_WAVE6 = cond(max_page < 75 & min_page > 49 & num_waves >=6, 1, 0)
gen AGE_50_74_WAVE3 = cond(max_page < 75 & min_page > 49 & num_waves >=3, 1, 0)

keep if kapick == 1
xtset kidid_group wave

// negative wealth shock
gen rneg_wealth = cond(hatotac/l.hatota < -.25, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth = cond(hatotac/l.hatota > .25, 1, 0) if !mi(l.hatota) & l.hatota < 0

gen rneg_wealth_50 = cond(hatotac/l.hatota < -.50, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth_50 = cond(hatotac/l.hatota > .50, 1, 0) if !mi(l.hatota) & l.hatota < 0

gen rneg_wealth_75 = cond(hatotac/l.hatota < -.75, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth_50 = cond(hatotac/l.hatota > .75, 1, 0) if !mi(l.hatota) & l.hatota < 0

gen rneg_wealth_100 = cond(hatotac/l.hatota < -1, 1, 0) if !mi(l.hatota) & l.hatota > 0
replace rneg_wealth_100 = cond(hatotac/l.hatota > 1, 1, 0) if !mi(l.hatota) & l.hatota < 0 

// negative wealth shock -- 25% rise in total debt
gen rdebt_inc = cond((real_total_debt - l.real_total_debt)/l.real_total_debt > .25, 1, 0)
replace rdebt_inc = . if mi(real_total_debt) | mi(l.real_total_debt)

// negative wealth shock  -- lots of debt
xtile rdebt_decile = real_total_debt, nq(10)
replace rdebt_decile = . if mi(real_total_debt)
gen rhigh_debt = rdebt_decile == 10

// any non-mortgage debt
gen rany_debt = (real_non_house_debt > 0)
replace rany_debt = . if mi(real_non_house_debt)

// percapita wealth and icome
gen real_hitot_pc =  real_hitot/(1 + hcpl)
gen real_hatota_pc =  real_hatota/(1 + hcpl)

xtile inc_quintile_pc = real_hitot_pc , nq(5)
xtile wealth_quintile_pc = real_hatota_pc , nq(5)
xtile inc_quintile = real_hitot , nq(5)
xtile wealth_quintile = real_hatota , nq(5)
replace real_hoopmd = real_roopmd if mi(real_hoopmd)
xtile hoopmd_quintile = real_hoopmd, nq(5)
gen ln_inc = ln(real_hitot_pc)
gen ln_wealth = ln(real_hatota_pc)

egen avg_lifetime_wealth_pc = mean(real_hatota_pc), by(hhidpn)
xtile wealthpctile_pc = avg_lifetime_wealth_pc, nq(2)

// # of kids in hhld
bysort hhidpn wave: gen nkids2 = _N

save data/constructed_data/kid_wave_all.dta, replace

